﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using CP.Entities;
using CP.Controllers.GPM;
using DevExpress.XtraEditors;
using CP.Utility;
using DevExpress.XtraEditors.Controls;

namespace GPM
{
    public partial class FrmEditMenu : BaseEditForm
    {
        MenuController controller = new MenuController();
        public int CurrentMenuID { get; private set; }
        public FrmEditMenu()
        {
            InitializeComponent();
            
        }

        public FrmEditMenu(string id, int menuid)
            : this()
        {
            base.EntityID = id;
            this.CurrentMenuID = menuid;
        }

        private void LoadImages()
        {
            for (int i = 0; i < imageListSmall.Images.Count; i++)
                imageCombo.Properties.Items.Add(new ImageComboBoxItem(i,i));
        }

        /// <summary>
        /// 编辑或者保存状态下取值函数
        /// </summary>
        /// <param name="info"></param>
        private void SetInfo(CP.Entities.Menu info)
        {
            info.menu_id = Convert.ToInt32(this.txtMenuID.Text);
            info.menu_name = this.txtMenuName.Text;
            info.parent_id = Convert.ToInt32(this.lookupParentID.EditValue ?? "0");
            info.webpart_id = Convert.ToInt32(this.lookupFormName.EditValue ?? "0");
            info.sort_order = Convert.ToInt32(this.txtSortOrder.Text);
            info.is_enable = this.chkEnable.Checked ? 1 : 0;
            info.image_index = Convert.ToInt32(this.imageCombo.EditValue??"0");
        }

        /// <summary>
        /// 数据字典加载
        /// </summary>
        private void InitDictItem()
        {
            this.lookupParentID.Properties.DataSource = controller.GetAllMenuParents();
            WebpartController wc =  new WebpartController();
            this.lookupFormName.Properties.DataSource = wc.GetAllWebparts();
            LoadImages();
        }

        public override bool CheckInput()
        {
            if (string.IsNullOrEmpty(this.txtMenuID.Text.Trim()))
            {
                ExMessageBox.Show("提示信息：菜单ID不能为空！", AppSetter.SoftName);
                return false;
            }

            if (string.IsNullOrEmpty(this.txtMenuName.Text.Trim()))
            {
                ExMessageBox.Show("提示信息：菜单名称不能为空！", AppSetter.SoftName);
                return false;
            }

            if (string.IsNullOrEmpty(this.txtSortOrder.Text.Trim()))
            {
                ExMessageBox.Show("提示信息：排序顺序不能为空！", AppSetter.SoftName);
                return false;
            }

            return true;
        }

        public override void DisplayData()
        {
            InitDictItem();

            if (!string.IsNullOrEmpty(EntityID))
            {
                CP.Entities.Menu info = controller.GetMenuByID(this.CurrentMenuID);
                #region 显示菜单信息

                this.txtMenuID.Focus();
                if (null != info)
                {
                    this.txtMenuID.Text = info.menu_id.ToString();
                    this.txtMenuID.Enabled = false;
                    this.txtMenuName.Text = info.menu_name;
                    this.lookupParentID.EditValue = info.parent_id;
                    this.lookupFormName.EditValue = info.webpart_id;
                    this.txtSortOrder.Text = info.sort_order.ToString();
                    this.chkEnable.Checked = info.is_enable == 1 ? true : false;
                    this.imageCombo.EditValue = info.image_index;
                }

                #endregion
            }
            else
            {
                this.txtMenuID.Focus();
            };
        }

        public override bool SaveAddNew()
        {
            CP.Entities.Menu menu = new CP.Entities.Menu();
            this.SetInfo(menu);
            controller.AddMenu(menu);
            LogHelper.Log("菜单管理", "新增", "", "menu:" +
                  menu.menu_id.ToString());
            (this.Owner.ActiveMdiChild as FrmMenu).RefreshMenu();
            return true;
        }

        public override bool SaveUpdated()
        {
            CP.Entities.Menu menu = new CP.Entities.Menu();
            this.SetInfo(menu);
            controller.EditMenu(menu);
            LogHelper.Log("菜单管理", "编辑", "", "menu:" +
             menu.menu_id.ToString());
            (this.Owner.ActiveMdiChild as FrmMenu).RefreshMenu();
            return true;
        }
    }
}
